package com.ziyun.erp.web.api.quoteGoods;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.PageUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ziyun.erp.modules.quoteParts.entity.QuotePartsEntity;
import com.ziyun.erp.modules.quoteParts.service.QuotePartsService;
import com.ziyun.erp.modules.quoteProduct.dto.QuoteProductBaseInfoDto;
import com.ziyun.erp.modules.quoteProduct.entity.QuoteProductBaseInfoEntity;
import com.ziyun.erp.modules.quoteProduct.service.QuoteProductBaseInfoService;
import com.ziyun.erp.modules.quoteProduct.vo.QuoteProductBaseInfoVO;

import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.modules.sys.entity.SysUserEntity;

/**
 * 产品基础信息关系表
 *
 * @author zh
 * @email 296588519@qq.com
 * @date 2018-09-05 15:37:34
 */
@RestController
@RequestMapping("port/quoteProduct/quoteproductbaseinfo")
public class QuoteProductBaseInfoControllerApi {
	@Autowired
	private QuoteProductBaseInfoService quoteProductBaseInfoService;

	@Autowired
	private QuotePartsService quotePartsService;

	/**
	 * 获取商品分类树
	 * 
	 * @return
	 */
	@RequestMapping("/getCategoryTree")
	@RequiresPermissions("quoteProduct:quoteproductbaseinfo:info")
	public R getAddInfo() {
		Map addInfoMap = quoteProductBaseInfoService.getAddInfo();
		return R.ok().put("addInfo", addInfoMap);
	}

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("quoteProduct:quoteproductbaseinfo:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = this.quoteProductBaseInfoService.queryQuoteBaseInfoPage(params);
		return R.ok().put("page", page);
	}

	/**
	 * 获取部件
	 * 
	 * @return
	 */
	@RequestMapping("/getParts")
	@RequiresPermissions("quoteProduct:quoteproductbaseinfo:info")
	public R getParts(Integer categoryId) {
		List<Map<String, Object>> partsList = quotePartsService.selectMaps(new EntityWrapper<QuotePartsEntity>()
				.setSqlSelect("id", "parts_name as partsName").eq("category_id", categoryId));
		return R.ok().put("partsList", partsList);
	}

	/**
	 * 获取分类属性选项
	 * 
	 * @return
	 */
	@RequestMapping("/getAttributeOption")
	@RequiresPermissions("quoteProduct:quoteproductbaseinfo:info")
	public R getAttributeOption(@RequestBody List<Integer> categoryIds) {
		Map attributeOptionMap = quoteProductBaseInfoService.getAttributeOption(categoryIds);
		return R.ok().put("attributeOptionMap", attributeOptionMap);
	}

	/**
	 * 获取已存在分类属性选项
	 * 
	 * @return
	 */
	@RequestMapping("/getExistAttributeOption")
	@RequiresPermissions("quoteProduct:quoteproductbaseinfo:info")
	public R getAttributeOption(Integer partsId, Integer categoryId) {
		Map attributeOptionMap = quoteProductBaseInfoService.getExistAttributeOption(partsId, categoryId);
		return R.ok().put("attributeOptionMap", attributeOptionMap);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("quoteProduct:quoteproductbaseinfo:info")
	public R info(@PathVariable("id") Integer id) {
		QuoteProductBaseInfoEntity quoteProductBaseInfo = quoteProductBaseInfoService.selectById(id);

		return R.ok().put("quoteProductBaseInfo", quoteProductBaseInfo);
	}

	/**
	 * 生成商品模板
	 */
	@RequestMapping("/generateProductTemplate")
	@RequiresPermissions("quoteProduct:quoteproductbaseinfo:save")
	public R generateProductTemplate(@RequestBody QuoteProductBaseInfoVO quoteProductBaseInfoVO) {
		// 获取当前登录人
		String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
		quoteProductBaseInfoVO.setCreateUser(username);
		quoteProductBaseInfoVO.setCreateDate(new Date());

		quoteProductBaseInfoService.generate(quoteProductBaseInfoVO);
		return R.ok();
	}

	/**
	 * 更改商品模板
	 */
	@RequestMapping("/updateProductTemplate")
	@RequiresPermissions("quoteProduct:quoteproductbaseinfo:update")
	public R updateProductTemplate(@RequestBody QuoteProductBaseInfoVO quoteProductBaseInfoVO) {
		// 获取当前登录人
		String username = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal()).getUsername();
		quoteProductBaseInfoVO.setCreateUser(username);
		quoteProductBaseInfoVO.setCreateDate(new Date());

		quoteProductBaseInfoVO.setUpdateUser(username);
		quoteProductBaseInfoVO.setUpdateDate(new Date());

		quoteProductBaseInfoService.updateProductTemplate(quoteProductBaseInfoVO);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("quoteProduct:quoteproductbaseinfo:delete")
	public R delete(@RequestBody Integer[] ids) {
		quoteProductBaseInfoService.deleteBatchIds(Arrays.asList(ids));

		return R.ok();
	}

	/**
	 * 根据分类获得所有模板表数据
	 */
	@RequestMapping("/quoteTemplate")
	public R getQuoteTemplate(@RequestBody Map paramBodyMap) {
		String categoryId = paramBodyMap.get("categoryId").toString();
		String factory = paramBodyMap.get("factoryId").toString();
		Integer requestParamInteger = StringUtil.getRequestParamInteger(categoryId);
		Integer factoryId = StringUtil.getRequestParamInteger(factory);
		StringUtil.getRequestParamInteger(categoryId);
		List<Map> allQuoteTemplate = quoteProductBaseInfoService.getAllQuoteTemplate(requestParamInteger,factoryId);

		return R.ok().put("allQuoteTemplate", allQuoteTemplate);
	}

}
